﻿Imports System
Imports System.Runtime.Serialization
Imports System.Xml

Namespace System.Workflow.ComponentModel.Serialization
    Friend NotInheritable Class XmlDocumentSurrogate
        Implements ISerializationSurrogate
        ' Methods
        Friend Sub New()
        End Sub

        Private Sub System.Runtime.Serialization.ISerializationSurrogate.GetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializationSurrogate.GetObjectData
            Dim document As XmlDocument = [TryCast](obj,XmlDocument)
            If (document Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "obj")
            End If
            info.AddValue("innerXml", document.InnerXml)
            info.SetType(GetType(XmlDocumentReference))
        End Sub

        Private Function System.Runtime.Serialization.ISerializationSurrogate.SetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext, ByVal selector As ISurrogateSelector) As Object Implements ISerializationSurrogate.SetObjectData
            Return Nothing
        End Function


        ' Nested Types
        <Serializable> _
        Private NotInheritable Class XmlDocumentReference
            Implements IObjectReference
            ' Methods
            Private Function System.Runtime.Serialization.IObjectReference.GetRealObject(ByVal context As StreamingContext) As Object Implements IObjectReference.GetRealObject
                Dim document As New XmlDocument
                If Not String.IsNullOrEmpty(Me.innerXml) Then
                    document.InnerXml = Me.innerXml
                End If
                Return document
            End Function


            ' Fields
            Private innerXml As String = String.Empty
        End Class
    End Class
End Namespace

